package org.webball.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.webball.base.interfaces.ICommunication;
import org.webball.communication.Communication;
/**
 * Thread responsible for accepting connections from clients
 * @author Marcin Kaczor
 *
 */
public class Listener extends Thread {
	
	private ServerSocket serverSocket= null;
	
	public Listener(int port) throws IOException{
		
			serverSocket= new ServerSocket(port);	
	}
	/**
	 * creates new user with communication thread based on socket and
	 * adds user to list
	 */
	public void run(){
		
		while(!interrupted()){
			
			try {
				Socket socket= serverSocket.accept();
				ICommunication communication= new Communication(socket);
				User user= new User(communication);
				Server.getInstance().addUser(user);
				communication.start();

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
